let app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id:null,
    match_id:null,
    match_type_id:null,
    matchtype:null,
    user_list:null,
    chooseUserId:0,
    chooseUserData:[],
    idtype_index:0,
    idtype_array:[ "请选择","身份证", "护照"],
    sex_index: 0,
    sex_array:["请选择","男","女"],
    user_idcard_type:0,
    user_sex:null,
    blood_array: ["请选择","O","A" ,"B","AB"],
    blood_index:0,
    birthdate: "2020-1-1",
    region: ["全部","全部","全部"],
    coat_array: ["请选择","XS", "S", "M", "L", "XL", "XXL", "XXXL"],
    coat_index:0,
    check:false,is_mine:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var open=wx.getStorageSync('open');
    console.log(open);
     var id=options.id;
    if(open==undefined ){
 
     wx.navigateTo({
       url: '../index/login',
     })
    }
    if(open==""){
 
     wx.navigateTo({
       url: '../index/index',
     })
    }
    let d=app.globalData;
            this.setData({
                open:open,
                platform:d.platform,    
                width:d.windowWidth,
                height:d.windowHeight,
                statusBarHeight:d.statusBarHeight,
                navHeight:d.navHeight,
                marginTop:d.navTitleTop,
                domain:d.domain,
                iUrl:d.iUrl,
                upUrl:d.upUrl
              })
  },
  goBack:function(){
    wx.navigateBack();
  },
  bindCoatChange:function(e) {
    
    var index=e.detail.value;
    console.log('picker发送选择改变，携带值为', index);
    var coat_array=this.data.coat_array;
    var coat=coat_array[index]
    this.setData({
     coat_index: index,
     coat:coat
    })
  },
  mineChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      is_mine: e.detail.value
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindCheckChange:function(e){
    this.data.check = !this.data.check;
  },
  bindCheckMine:function(e){
    var is_mine=this.data.is_mine;
    if(is_mine ==1){
        is_mine = 0;
    }else{
        is_mine = 1;
    }
    this.setData({
        is_mine:is_mine
    })
  },
  bindIdcardChange: function (e) {
    ////console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      idtype_index: e.detail.value
    })
  },
  bindBloodChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      blood_index: e.detail.value
      
    })
  },
  bindPhone(e){
    console.log(phone);
  },
  chooseUser:function(e){
    //console.log(e);
    var userid = e.target.dataset.userid;
    var chooseuserindex = e.target.dataset.key;
    var chooseUserData = this.data.user_list[chooseuserindex];
    this.data.user_id=userid;
    //console.log(chooseUserData);
    this.setData({
      chooseUserId: userid,
      chooseUserData: chooseUserData,
      idtype_index: chooseUserData["user_idcard_type"],
      sex_index: chooseUserData["user_sex"],
      user_blood: chooseUserData["user_blood"],
      region:[chooseUserData["user_province"], chooseUserData["user_city"], chooseUserData["user_area"]],
      coat_index: chooseUserData["user_coat"]
    })

  },
  shouErrorMsg:function(e){
    wx.showToast({
      title: e,
      image:'../../icon/fail.png'
    })
  },
  validPhoneNum(num){
    if (!(/^1[345789]\d{9}$/.test(num))) {
      return false;
    }else{
      return true;
    }
  },
  validName:function(e){
    if(e){
      if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(e))) {
        return false;
      }else{
        return true;
      }
   }else{
      return false;
   }

  },
  validIdcardNum:function(e){
    var StrNo = e.toString()
	if (StrNo.length == 18) {
		var a, b, c
		if (!this.isInteger(StrNo.substr(0, 17)))
			return false;

		a = parseInt(StrNo.substr(0, 1)) * 7 + parseInt(StrNo.substr(1, 1)) * 9 + parseInt(StrNo.substr(2, 1)) * 10;
		a = a + parseInt(StrNo.substr(3, 1)) * 5 + parseInt(StrNo.substr(4, 1)) * 8 + parseInt(StrNo.substr(5, 1)) * 4;
		a = a + parseInt(StrNo.substr(6, 1)) * 2 + parseInt(StrNo.substr(7, 1)) * 1 + parseInt(StrNo.substr(8, 1)) * 6;
		a = a + parseInt(StrNo.substr(9, 1)) * 3 + parseInt(StrNo.substr(10, 1)) * 7 + parseInt(StrNo.substr(11, 1)) * 9;
		a = a + parseInt(StrNo.substr(12, 1)) * 10 + parseInt(StrNo.substr(13, 1)) * 5 + parseInt(StrNo.substr(14, 1)) * 8;
		a = a + parseInt(StrNo.substr(15, 1)) * 4 + parseInt(StrNo.substr(16, 1)) * 2;
		b = a % 11;

		if (b == 2)
			c = StrNo.substr(17, 1).toUpperCase(); //转为大写X 
		else
			c = parseInt(StrNo.substr(17, 1));

		switch (b) {
			case 0:
				if (c != 1)
					return false;
				break;
			case 1:
				if (c != 0)
					return false;
				break;
			case 2:
				if (c != "X")
					return false;
				break;
			case 3:
				if (c != 9)
					return false;
				break;
			case 4:
				if (c != 8)
					return false;
				break;
			case 5:
				if (c != 7)
					return false;
				break;
			case 6:
				if (c != 6)
					return false;
				break;
			case 7:
				if (c != 5)
					return false;
				break;
			case 8:
				if (c != 4)
					return false;
				break;
			case 9:
				if (c != 3)
					return false;
				break;
			case 10:
				if (c != 2)
					return false;
		}
	}
	else //15位身份证号 
	{
		if (!this.isInteger(StrNo))
			return false;
	}

	switch (StrNo.length) {
		case 15:
			if (this.isValidDate("19" + StrNo.substr(6, 2), StrNo.substr(8, 2), StrNo.substr(10, 2)))
				return true;
			else
				return false;
		case 18:
			if (this.isValidDate(StrNo.substr(6, 4), StrNo.substr(10, 2), StrNo.substr(12, 2)))
				return true;
			else
				return false;
	}

	return false

  },
isInteger:function(str) {
    if (/[^\d]+$/.test(str)) {
      return false;
    }
    return true;
  },
  isValidDate:function(iY, iM, iD) {
    var a = new Date(iY, iM - 1, iD);
    var y = a.getFullYear();
    var m = a.getMonth() + 1;
    var d = a.getDate();
    if (y != iY || m != iM || d != iD)
      return false;
    return true
  },

  validEmail:function(e){
    console.log(e);
    if (!(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(e))) {
     return false;
    }else{
      return true;
    }
  },

  formSubmit:function(e){
    console.log(e);
    const that=this;
    var check=this.data.check;
    var is_mine=this.data.is_mine;
    var inputArray=e.detail.value;
    console.log(inputArray);
    var open=wx.getStorageSync('open'); 
    if(check){
      if (!this.validName(inputArray.user_name)){
        this.shouErrorMsg("用户名错误");
        this.setData({
          user_name_focus:true
        })
      } else if (!this.validPhoneNum(inputArray.user_tel)){
        this.shouErrorMsg("电话号码错误");
        this.setData({
          user_tel_focus: true
        })
      } else if (inputArray.idtype_index == 0){
        this.shouErrorMsg("请选择证件类型");
      } else if (!this.validIdcardNum(inputArray.user_idcard)){
        this.shouErrorMsg("请证件号码有误");
        this.setData({
          user_idcard_focus: true
        })
      } else if (!this.validEmail(inputArray.email)){
        this.shouErrorMsg("邮箱不正确");      
    } else if (inputArray.blood_index == 0){
        this.shouErrorMsg("请选择血型");      
    } else if (inputArray.coat_index == 0){
        this.shouErrorMsg("请选择衣服尺码");
    }else if (inputArray.user_address=="") {
        this.shouErrorMsg("住址错误");
        this.setData({
          user_address_focus: true
        })
      } else if (!this.validName(inputArray.user_contact)) {
        this.shouErrorMsg("紧急联系人错误");
        this.setData({
          user_contact_focus: true
        })
      } else if (!this.validPhoneNum(inputArray.user_contact_tel)){
        this.shouErrorMsg("紧急电话错误");
        this.setData({
          user_contact_tel_focus: true
        })
      }else{
        var open=wx.getStorageSync('open');
        console.log(open);
         var unionid=open.unionid;
        

        
        wx.request({
         // url: 'https://lypb.com/lupao/enrolls.php',
          url: app.globalData.domain+'/wechat/user/add',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          data: { 
        
                from:1, 
                open_id:open.id,
                is_mine:inputArray.is_mine,
                name: inputArray.user_name,
                phone: inputArray.user_tel, 
                idcard_type: inputArray.user_idcard_type, 
                idcard: inputArray.user_idcard, 
                email: inputArray.email, 
                coat: inputArray.coat_index,          
                province:inputArray.user_province,
                city:inputArray.user_city,
                area:inputArray.user_area,
                address:inputArray.user_address,
                link_name:inputArray.user_contact,                 
                link_phone:inputArray.user_contact_tel,
                blood:inputArray.blood_index,
            },
          success:function(res){
              console.log(res)
            if (res.data.code==200){
               wx.navigateBack();
        
              }else{
                wx.showModal({
                  title: 'fail',
                  content: res.data.msg,
                })
              }
          },
          fail:function(res){

          }
        })

      } 
    }else{
      this.shouErrorMsg("请同意协议")
    }

  },
  getIndex(){

  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})